
using System;
using UMC.Net;
using System.Text;

namespace UMC.ITME
{

    public class HttpAppender
    {
        StringValue _ConfValue;
        int _appendIndex = -1;

        public int AppendIndex => _appendIndex;

        public int Index = 1;// => _appendIndex;
        bool _IsStop;
        public bool IsStop => _IsStop;

        bool _isEnd = false;
        int _start = 0;


        public void Append(NetWriteData writeData)
        {
            if (this._ConfValue.Length > 0)
            {
                int outStart = _start;

                bool isStart = false;
                var span = this._ConfValue.Span;
                for (var i = _start; i < this._ConfValue.Length; i++)
                {
                    switch (span[i])
                    {
                        case 32:
                        case 10:
                        case 13:
                        case 9:
                            isStart = false;
                            break;
                        // case '{':
                        case 123:
                            isStart = true;
                            // writeData(this._ConfValue, outStart, i - outStart);

                            this._ConfValue.Slice(outStart, i - outStart).Output(writeData);
                            outStart = i;
                            break;
                        // case '}':
                        case 125:
                            if (isStart)
                            {
                                isStart = false;
                                var value = this.httpRequest.Headers.Get(this._ConfValue.Slice(outStart + 1, i - outStart - 1).Span);
                                if (value.IsEmpty)
                                {
                                    this._ConfValue.Slice(outStart, i - outStart).Output(writeData);
                                    // writeData(this._ConfValue, outStart, i - outStart);
                                    outStart = i;
                                }
                                else
                                {
                                    value.Output(writeData);
                                    outStart = i + 1;
                                }
                            }
                            else
                            {
                                this._ConfValue.Slice(outStart, i - outStart).Output(writeData);
                                // writeData(this._ConfValue, outStart, i - outStart);
                                outStart = i;
                            }
                            break;
                    }
                }

                if (outStart < this._ConfValue.Length)
                    this._ConfValue.Slice(outStart, this._ConfValue.Length - outStart).Output(writeData);
                // writeData(this._ConfValue, outStart, this._ConfValue.Length - outStart);

                this._ConfValue = StringValue.Empty;
            }
            if (_isEnd)
            {
                _IsStop = true;
            }
        }
        NetHttpRequest httpRequest;
        public HttpAppender(NetHttpRequest req, String mainKey)
        {

            _ConfValue = UMC.Data.DataFactory.Instance().Config(mainKey)?.ConfValue ?? StringValue.Empty;//.Span.ToArray();//.UTF8();
            if (_ConfValue.Length > 0)
            {
                var span = this._ConfValue.Span;
                this.httpRequest = req;
                if (span[0] == ':')
                {
                    var s = span.IndexOf((byte)'\n');
                    var su = span.Slice(1, s - 1);
                    if (su.EndsWith("$"u8))
                    {
                        _isEnd = true;
                        su = su.Slice(0, su.Length - 1);
                    }
                    if (UMC.Data.Utility.TryParse(su, 10, out _appendIndex) == false)
                    {
                        _appendIndex = -1;
                    }
                    _start = s + 1;
                }
            }
            else
            {
                _IsStop = true;
            }
        }
    }
}